package com.esd.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.esd.common.pojo.R;
import com.esd.device.entity.ChannelType;
import com.esd.device.entity.DeviceChannel;
import com.esd.device.entity.vo.DeviceChannelVo;
import com.esd.device.mapper.ChannelTypeMapper;
import com.esd.device.mapper.DeviceChannelMapper;
import com.esd.device.service.ChannelTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class ChannelTypeServiceImpl extends ServiceImpl<ChannelTypeMapper , ChannelType> implements ChannelTypeService {

    @Override
    public R addChannelType(ChannelType channelType) {
        LambdaQueryWrapper<ChannelType> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.eq(ChannelType::getChannelTypeName, channelType.getChannelTypeName());
        ChannelType channelType1 = baseMapper.selectOne(queryWrapper);
        if (channelType1 != null) {
            return R.error("该通道类型已存在，请查询编辑！！");
        }
        baseMapper.insert(channelType);
        return R.ok("添加通道类型成功！！");
    }

    @Autowired
    private DeviceChannelMapper deviceChannelMapper ;

    @Override
    public R deleteChannelType(Long channelTypeId) {
        LambdaQueryWrapper<DeviceChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceChannel::getChannelTypeId,channelTypeId) ;
        DeviceChannel deviceChannel = deviceChannelMapper.selectOne(queryWrapper, false);
        if (deviceChannel != null){
            return R.error("存在关联的终端，不能删除！！");
        }
        baseMapper.deleteById(channelTypeId);
        return R.ok("删除通道类型成功！！");
    }

    @Autowired
    private ChannelTypeMapper channelTypeMapper ;

    //  旧版的双通道电阻仪 获取通道类型
//    @Override
//    public List<String> getChannelType(Long deviceId) {
//        return channelTypeMapper.getChannelTypeName(deviceId);
//    }
//
//    @Override
//    public List<HashMap<String, Object>> getChannelType1(Long deviceId) {
//        return channelTypeMapper.getChannelTypeName1(deviceId);
//    }

    @Override
    public R updateChannelType(ChannelType channelType) {
        LambdaQueryWrapper<ChannelType> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.eq(ChannelType::getChannelTypeName, channelType.getChannelTypeName());
        ChannelType channelType1 = baseMapper.selectOne(queryWrapper);
        if (channelType1 != null && !Objects.equals(channelType1.getChannelTypeId(), channelType.getChannelTypeId())) {
            return R.error("该通道类型已存在，请查询编辑！！");
        }
        baseMapper.updateById(channelType);
        return R.ok("编辑通道类型成功！！");
    }

    @Override
    public ChannelType getChannelTypeByName(String channelTypeName) {
        LambdaQueryWrapper<ChannelType> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.eq(ChannelType::getChannelTypeName, channelTypeName);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<DeviceChannelVo> getListByDeviceId(Long deviceId) {
        return channelTypeMapper.getListByDeviceId(deviceId);
    }
}
